package com.hotchpotch.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hotchpotch.R;
import com.hotchpotch.inter.OnDeletePopClickListener;
import com.hotchpotch.util.MyUtil;

/**
 * Created by Administrator on 2017/5/19 0019.
 */

public class DeletePopwindow extends PopupWindow {
    private Context mContext;
    //屏幕的宽度和高度
    private int mScreenWidth, mScreenHeight;
    //位置不在中心
    private int popupGravity = Gravity.CENTER;
    //自己定义的点击监听
    private OnDeletePopClickListener onDeletePopClickListener;
    private TextView textViewContent, textViewCancel, textViewConfirm;
    private String content = "";
    private String confirm = "";
    private String cancel = "";

    public DeletePopwindow(Context context, String content) {
        //设置布局的参数
        this(context, content, "取消", "确定");
    }

    public DeletePopwindow(Context context, String content, String cancel, String confirm) {
        this.mContext = context;
        this.content = content;
        this.confirm = confirm;
        this.cancel = cancel;

        //设置可以获得焦点
        setFocusable(true);
        //设置弹窗内可点击
        setTouchable(true);
        //设置弹窗外可点击
        setOutsideTouchable(true);
        //设置动画效果
        setAnimationStyle(R.style.anim_scale_alpha);

        //获得屏幕的宽度和高度
        mScreenWidth = MyUtil.getScreenWidth(mContext);
        mScreenHeight = MyUtil.getScreenHeight(mContext);
        //设置弹窗的宽度和高度
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new BitmapDrawable());
        //设置弹窗的布局界面
        setContentView(LayoutInflater.from(mContext).inflate(R.layout.pop_delete, null));
        initUI();
    }

    /**
     * 初始化弹窗列表
     */
    private void initUI() {
        textViewContent = (TextView) getContentView().findViewById(R.id.pop_delete_content);
        textViewCancel = (TextView) getContentView().findViewById(R.id.pop_delete_cancel);
        textViewCancel.setOnClickListener(clickListener);
        textViewConfirm = (TextView) getContentView().findViewById(R.id.pop_delete_confirm);
        textViewConfirm.setOnClickListener(clickListener);
        textViewContent.setText(content);
        textViewCancel.setText(cancel);
        textViewConfirm.setText(confirm);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //点击子类项后，弹窗消失
            dismiss();
            if (onDeletePopClickListener != null) {
                switch (v.getId()) {
                    case R.id.pop_delete_cancel://取消
                        onDeletePopClickListener.cancel();
                        break;
                    case R.id.pop_delete_confirm://确定
                        onDeletePopClickListener.confirm();
                        break;
                }
            }
        }
    };

    /**
     * 显示弹窗列表界面
     */
    public void showView(View view) {
        //显示弹窗的位置
        showAtLocation(view, popupGravity, 0, 0);
        backgroundAlpha((Activity) mContext, 0.4f);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) mContext, 1f);
            }
        });
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    /**
     * 自己添加的点击监听
     */
    public void setPopDeleteOnClickListener(OnDeletePopClickListener onDeletePopClickListener) {
        this.onDeletePopClickListener = onDeletePopClickListener;
    }
}
